package com.xuecheng.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.api.system.model.dto.DictionaryDTO;
import com.xuecheng.system.convert.DictionaryConvert;
import com.xuecheng.system.entity.Dictionary;
import com.xuecheng.system.mapper.DictionaryMapper;
import com.xuecheng.system.service.DictionaryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 数据字典 服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class DictionaryServiceImpl extends ServiceImpl<DictionaryMapper, Dictionary> implements DictionaryService {

    @Override
    public List<DictionaryDTO> queryAll() {
        //查询全部的数据
        List<Dictionary> list = this.list();
        //转换并返回
        return DictionaryConvert.INSTANCE.entities2dtos(list);

    }

    @Override
    public DictionaryDTO getDicByCode(String code) {
        //判断数据的合法性
        if (StringUtils.isEmpty(code)){
            return null;
        }
        //根据条件查询
        Dictionary dictionary = this.getOne(new LambdaQueryWrapper<Dictionary>().eq(Dictionary::getCode,code));
        if (ObjectUtils.isEmpty(dictionary)){
            return new DictionaryDTO();
        }

        return DictionaryConvert.INSTANCE.entity2dto(dictionary);
    }
}
